rm(list = ls())
setwd("/Users/John/Dropbox/")

# --- Load Required Packages ---
library(lfe)
library(readr)
library(texreg)

# Load Data ---
df <- as.data.frame(read_csv("JOP_Replication_Materials/appendix/data/final_dataset_marketsize.csv"))
df <- df[,-1]

# --- OLS models ---
msize1 <- felm(sqrtta ~ nonept_imp_worldshare | 0 | 0 | isic + year, data = df)
msize2 <- felm(sqrtta ~ total_cnimp_worldshare | 0 | 0 | isic + year, data = df)
msize3 <- felm(sqrtta ~ nonept_share_lead4 | 0 | 0 | isic + year, data = df)
msize4 <- felm(sqrtta ~ nonept_share_lead8 | 0 | 0 | isic + year, data = df)
msize5 <- felm(sqrtta ~ total_cn_world_share4 | 0 | 0 | isic + year, data = df)
msize6 <- felm(sqrtta ~ total_cn_world_share8 | 0 | 0 | isic + year, data = df)

# --- Coefficient name mapping ---
coef_map <- list(
  "nonept_imp_worldshare" = "China Share (excl. Process)",
  "total_cnimp_worldshare" = "Total China Share",
  "nonept_share_lead4" = "China Share (excl. Process), 4-Yr Lead",
  "nonept_share_lead8" = "China Share (excl. Process), 8-Yr Lead",
  "total_cn_world_share4" = "Total China Share, 4-Yr Lead",
  "total_cn_world_share8" = "Total China Share, 8-Yr Lead",
  "(Intercept)" = "Constant"
)

# --- Custom GOF rows ---
custom_gof <- list(
  "Num. Obs." = c(nobs(msize1), nobs(msize2), nobs(msize3), nobs(msize4), 
                  nobs(msize5), nobs(msize6)),
  "R$^2$" = c(
    round(summary(msize1)$r.squared, 3),
    round(summary(msize2)$r.squared, 3),
    round(summary(msize3)$r.squared, 3),
    round(summary(msize4)$r.squared, 3),
    round(summary(msize5)$r.squared, 3),
    round(summary(msize6)$r.squared, 3)
  ),
  "Adj. R$^2$" = c(
    round(summary(msize1)$adj.r.squared, 3),
    round(summary(msize2)$adj.r.squared, 3),
    round(summary(msize3)$adj.r.squared, 3),
    round(summary(msize4)$adj.r.squared, 3),
    round(summary(msize5)$adj.r.squared, 3),
    round(summary(msize6)$adj.r.squared, 3)
  ),
  "Log Likelihood" = c(
    round(logLik(msize1), 3),
    round(logLik(msize2), 3),
    round(logLik(msize3), 3),
    round(logLik(msize4), 3),
    round(logLik(msize5), 3),
    round(logLik(msize6), 3)
  )
)

# --- Custom extractor for glm with cluster-robust SEs ---
extract.robust <- function(model, robust_se) {
  coefs <- coef(model)
  se <- robust_se[, 2]
  pval <- robust_se[, 4]
  names <- rownames(robust_se)
  
  gof <- numeric(0)
  gof_names <- character(0)
  gof_decimal <- logical(0)
  
  # Try logLik
  ll <- suppressWarnings(tryCatch(as.numeric(logLik(model)), error = function(e) NA_real_))
  if (!is.na(ll)) {
    gof <- c(gof, ll)
    gof_names <- c(gof_names, "Log Likelihood")
    gof_decimal <- c(gof_decimal, TRUE)
  }
  
  # Try Num. Obs.
  n <- suppressWarnings(tryCatch(nobs(model), error = function(e) NA_real_))
  if (length(n) == 1 && !is.na(n)) {
    gof <- c(gof, n)
    gof_names <- c(gof_names, "Num. Obs.")
    gof_decimal <- c(gof_decimal, FALSE)
  }
  
  createTexreg(
    coef.names = names,
    coef = coefs[names],
    se = se,
    pvalues = pval,
    gof.names = gof_names,
    gof = gof,
    gof.decimal = gof_decimal
  )
}

# --- Strip GOF from a model ---
strip_all_gof <- function(model) {
  model@gof.names <- character(0)
  model@gof <- numeric(0)
  model@gof.decimal <- logical(0)
  return(model)
}

# --- Model list ---
models <- list(
  strip_all_gof(texreg::extract(msize1)),
  strip_all_gof(texreg::extract(msize2)),
  strip_all_gof(texreg::extract(msize3)),
  strip_all_gof(texreg::extract(msize4)),
  strip_all_gof(texreg::extract(msize5)),
  strip_all_gof(texreg::extract(msize6))
)

# --- Create LaTeX file from texreg ---
tex_output <- "JOP_Replication_Materials/appendix/output/E_table_6_plain.tex"

texreg(
  models,
  file = tex_output,
  custom.coef.map = coef_map,
  stars = c(0.1, 0.05, 0.01),
  digits = 3,
  custom.gof.rows = custom_gof,
  caption = "Regression Results on Tech Absorption",
  label = "tab:regression_results"
)

# --- Wrap LaTeX for full PDF ---
table_tex_raw <- readLines(tex_output)
start_line <- grep("\\\\begin\\{tabular\\}", table_tex_raw)
end_line <- grep("\\\\end\\{tabular\\}", table_tex_raw)
table_tex <- table_tex_raw[start_line:end_line]

latex_wrapper <- c(
  "\\documentclass[11pt]{article}",
  "\\usepackage[margin=1in]{geometry}",
  "\\usepackage{booktabs}",
  "\\usepackage{caption}",
  "\\usepackage{graphicx}",
  "\\usepackage{float}",
  "\\usepackage{adjustbox}",
  "\\usepackage{amsmath}",
  "\\usepackage{longtable}",
  "\\usepackage{placeins}",
  "\\usepackage{mathptmx}",  
  "\\renewcommand{\\arraystretch}{0.6}",
  "\\begin{document}",
  "\\begin{table}[H]",
  "\\centering",
  "\\caption{Restuls from Regression of Tech Absorption Policies on Present, Future Market Size}",
  "\\vspace{0.5em}",
  "\\resizebox{\\textwidth}{!}{%",
  table_tex,
  "}",
  "\\end{table}",
  "\\FloatBarrier",
  "\\end{document}"
)

wrapped_file <- "JOP_Replication_Materials/appendix/output/E_table_6_wrapped.tex"
writeLines(latex_wrapper, wrapped_file)

# --- Save PDF ---
tools::texi2pdf(wrapped_file, clean = TRUE)
file.rename("E_table_6_wrapped.pdf", "JOP_Replication_Materials/appendix/output/E_table_6_wrapped.pdf")
browseURL("JOP_Replication_Materials/appendix/output/E_table_6_wrapped.pdf")